<?php
/**
 * @description : create our client object.
 * @class-name : createObjectOfClientJob();
 * @author : stephen.mo <stephen.mo@achievo.com>
 * @date : 2010-03-29
 */

class createObjectOfClientJob {
    private $config;
    private $reader;
    private $writer;
    private $gmclient;

    const APC_KEY_NAME = 'gearman_client_hosts_ports';

    public static function getInstance($server) {
        static $instance = null ;
        return $instance ? ($instance) : ($instance = new createObjectOfClientJob($server)) ;
    }

    public function __construct($server) {
        //$this->config = ConfigReaderFactory::getInstance()->getConfigReader(GearmandConfig::SERVER_ADDRESS_CONFIG)->getConfig();
        $this->init($server,self::APC_KEY_NAME);
    }
    
    //get apc name.
    private function getApcData($key) {
        $data = apc_fetch($key);
        return $data;
    }
    
    //set the error.
    private function error() {
        echo "Maybe your servers wasn't start gearmand! pls restart it again! ";
        die();
    }

    //init this gearmand.
    public function init($servers,$apc_key) {
        $data = array() ;
        $data = $this->getApcData($apc_key);

        if (empty($data)) {
            $host_obj = $servers;
            
            //get the servers with loop.
            foreach($host_obj as $key=>$value) {
                $host_name = trim((string)$value['host']);
                $host_port = trim((string)$value['port']);
                if (empty($host_name)) continue;
                $hostlist = (!empty($host_port)) ? $host_name.":".$host_port : $host_name;
                $data[] = $hostlist;
            }
            //set value to apc.
            apc_store($apc_key,$data);
        }
		
		//add the servers from xml file or apc cache.
        $this->gmclient= new GearmanClient();
        foreach($data as $key=>$value) {
            $result = $this->gmclient->addServer($value);
        }
    }


    //do job normal.
    public function doNormal($fn_name,$workload,$unique=null) {
        if (is_array($workload)) $workload = json_encode($workload);
        $result = @$this->gmclient->do($fn_name,$workload,$unique);
        if (empty($result)) $this->error();
        return ($this->gmclient->returnCode()!= GEARMAN_SUCCESS) ? false : true;
    }
    
    //do job on background.
    public function doNormalBackground($fn_name,$workload,$unique=null) {
        if (is_array($workload)) $workload = json_encode($workload);
        $result = @$this->gmclient->doBackground($fn_name,$workload,$unique);
        if (empty($result)) $this->error();
        return ($this->gmclient->returnCode()!= GEARMAN_SUCCESS) ? false : true;
    }
    
    //do job with high priority.
    public function doHigh($fn_name,$workload,$unique=null) {
        if (is_array($workload)) $workload = json_encode($workload);
        $result = $this->gmclient->do($fn_name,$workload,$unique);
        return ($this->gmclient->returnCode()!= GEARMAN_SUCCESS) ? false : true;
    }
    
    //do job with low priority.
    public function doLow($fn_name,$workload,$unique=null) {
        if (is_array($workload)) $workload = json_encode($workload);
        $result = @$this->gmclient->do($fn_name,$workload,$unique);
        if (empty($result)) $this->error();
        return ($this->gmclient->returnCode()!= GEARMAN_SUCCESS) ? false : true;
    }

    //do job with high priority on background.
    public function doHighBackground($fn_name,$workload,$unique=null) {
        if (is_array($workload)) $workload = json_encode($workload);
	    $result = $this->gmclient->do($fn_name,$workload,$unique);
        if (empty($result)) $this->error();
        return ($this->gmclient->returnCode()!= GEARMAN_SUCCESS) ? false : true;
    }
    
    //do job with low priority on background.
    public function doLowBackground($fn_name,$workload,$unique=null) {
        if (is_array($workload)) $workload = json_encode($workload);
        $result = @$this->gmclient->do($fn_name,$workload,$unique);
        if (empty($result)) $this->error();
        return ($this->gmclient->returnCode()!= GEARMAN_SUCCESS) ? false : true;
    }
    
    //get job handle.
    public function doJobHandle() {
        return @$this->gmclient->doJobHandle();
    }
    
    //get the job status.
    public function doStatus() {
        return @$this->gmclient->doStatus();
    }


    //add task for gearmand.
    public function addTask($name,$workload,&$context=null,$unique=null) {
        if (is_array($workload)) $workload = json_encode($workload);
        $task = @$this->gmclient->addTask($name,$workload,$context,$unique);
        if (empty($task)) $this->error();
        return $task;
    }

    //add task on background.
    public function addTaskBackground($name,$workload,&$context=null,$unique=null) {
        if (is_array($workload)) $workload = json_encode($workload);
        $task = @$this->gmclient->addTaskBackground($name,$workload,$context,$unique);
        if (empty($task)) $this->error();
        return $task;
    }
    
    //add task with high priority.
    public function addTaskHigh($name,$workload,&$context=null,$unique=null) {
        if (is_array($workload)) $workload = json_encode($workload);
        $task = @$this->gmclient->addTaskBackground($name,$workload,$context,$unique);
        if (empty($task)) $this->error();
        return $task;
    }
    
    //add task with high priority on background.
    public function addTaskHighBackground($name,$workload,&$context=null,$unique=null) {
        if (is_array($workload)) $workload = json_encode($workload);
        $task = @$this->gmclient->addTaskBackground($name,$workload,$context,$unique);
        if (empty($task)) $this->error();
        return $task;
    }
    
    //add task with low priority.
    public function addTaskLow($name,$workload,&$context=null,$unique=null) {
        if (is_array($workload)) $workload = json_encode($workload);
        $task = @$this->gmclient->addTaskBackground($name,$workload,$context,$unique);
        if (empty($task)) $this->error();
        return $task;
    }
    
    //add task with low priority on background.
    public function addTaskLowBackground($name,$workload,&$context=null,$unique=null) {
        if (is_array($workload)) $workload = json_encode($workload);
        $task = @$this->gmclient->addTaskBackground($name,$workload,$context,$unique);
        if (empty($task)) $this->error();
        return $task;
    }
    
    //run task.
    public function runTasks() {
        return $this->gmclient->runTasks();
    }


}

?>
